package com.sunday.aomicaichang.adpter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.sunday.common.imageselector.MultiImageSelectorActivity;
import com.sunday.common.widgets.explosionfield.ExplosionField;
import com.sunday.common.widgets.gridview.NoScrollGridView;
import com.sunday.aomicaichang.R;
import com.sunday.aomicaichang.model.OrderAssess;
import com.sunday.aomicaichang.widgets.FlexibleRatingBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by yun_sheng on 2017/6/2.
 */

public class OrderAssessAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener{


    private Context mContext;
    private List<OrderAssess> assessList;
    private Activity activity;
    private final int REQUEST_CODE_LICENCE = 1;
    private ExplosionField mExplosionField;
    private ArrayList<String> mSelectPath ;
    private MakeCommentImageAdapter adapter;
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;
    public OrderAssessAdapter(Context mContext, Activity activity,List<OrderAssess> assessList,
                             ArrayList<String> mSelectPath) {
        this.mContext = mContext;
        this.activity=activity;
        this.assessList = assessList;
        this.mSelectPath=mSelectPath;
        mExplosionField = ExplosionField.attach2Window(activity);
        if(adapter!=null)
        adapter.notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_assess, parent, false);
        return new ItemViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final OrderAssess assess = assessList.get(position);
        final ItemViewHolder viewHolder = (ItemViewHolder) holder;
        if (assess.getImg() != null)
            Picasso.with(mContext)
                    .load(assess.getImg())
                    .error(R.mipmap.ic_product)
                    .into(viewHolder.imgTop);
        if (assess.getName() != null)
            viewHolder.tvName.setText(assess.getName());
        if (assess.getContent() != null)
            viewHolder.editText.setText(assess.getContent());
        else {
            viewHolder.editText.setText("");
        }
        if(assess.getScore()!=0){
            viewHolder.flexibleRatingBar.setRating(assess.getScore());
        }else {
            viewHolder.flexibleRatingBar.setRating(0);
        }


        viewHolder.flexibleRatingBar.setTag(position);
        viewHolder.flexibleRatingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                assessList.get(position).setScore(rating);
            }
        });
        viewHolder.editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(viewHolder.editText.getText().toString()!=null)
                   assess.setContent(viewHolder.editText.getText().toString());
            }
        });

        adapter=new  MakeCommentImageAdapter(mContext,assess.getUrl());
        viewHolder.images.setAdapter(adapter);
        adapter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Integer pos = (Integer)v.getTag();
                if(v.getId()==R.id.delete_image){
                    assess.getUrl().remove((int)pos);
                    adapter.notifyDataSetChanged();
                    notifyDataSetChanged();
                    View view = (View)v.getTag(R.id.action0);
                    mExplosionField.explode(view);
                    return ;
                }

                if(pos==assess.getUrl().size()-1){
                    if(mSelectPath!=null){
                        mSelectPath.clear();
                    }
                   Intent intent = new Intent(mContext, MultiImageSelectorActivity.class);
                    // 是否显示拍摄图片
                    intent.putExtra(MultiImageSelectorActivity.EXTRA_SHOW_CAMERA, true);
                    // 最大可选择图片数量
                    intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_COUNT, 3);
                    // 选择模式
                    intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_MODE, MultiImageSelectorActivity.MODE_MULTI);
                    // 默认选择
                    if(mSelectPath != null && mSelectPath.size()>0){
                        intent.putExtra(MultiImageSelectorActivity.EXTRA_DEFAULT_SELECTED_LIST, mSelectPath);
                    }
                    intent.putExtra("position",position);
                    activity.startActivityForResult(intent, pos);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return assessList == null ? 0 : assessList.size();
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取数据
            mOnItemClickListener.onItemClick(v, (int) v.getTag());

        }
    }

    public interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, int position);
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
    public class ItemViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.img_top)
        ImageView imgTop;
        @Bind(R.id.tv_name)
        TextView tvName;
        @Bind(R.id.flexibleRatingBar)
        FlexibleRatingBar flexibleRatingBar;
        @Bind(R.id.edit_text)
        EditText editText;
        @Bind(R.id.images)
        NoScrollGridView images;
        public ItemViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

}
